package com.zr.daoimpl;

import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.zr.dao.LineDaoLPN;
import com.zr.model.Function;
import com.zr.model.Line;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
@Repository("lineDao")
public class LineDaoImplLPN extends BaseDaOImpl implements LineDaoLPN{

	@Override
	public JSONArray getline(int page,int rows) {
		JSONArray j = new JSONArray();
		String hql  = "from Line";
		Query query = this.getCurrentSession().createQuery(hql);
		query.setFirstResult((page-1)*rows);
		query.setMaxResults(rows);
		List<Line> line = query.list();
		for (Line li : line) {
			JSONObject jo = new JSONObject();
			jo.put("lineId",li.getLineId());
			jo.put("lineStartPoint",li.getLineStartPoint() );
			jo.put("lineEndPoint",li.getLineEndPoint() );
			jo.put("lineTransportationType",li.getLineTransportationType() );
			j.add(jo);
		}
		return j;
	}

	@Override
	public int getLineCount() {
		String hql = "select count(l.lineId) from Line  l";
    	Query query = this.getCurrentSession().createQuery(hql);
    	int count = ((Number) query.uniqueResult()).intValue();
		return count;
	}

	@Override
	public JSONArray findTranSta1( String lineEndPoint) {
		JSONArray j = new JSONArray();
		String hql  = "select  l.lineStartPoint from Line as l where l.lineEndPoint=:lineEndPoint ";
		Query query = this.getCurrentSession().createQuery(hql);
		query.setString("lineEndPoint", lineEndPoint);
		
		List lines = query.list();
		System.out.println(lines);
		Iterator it = lines.iterator();
		while (it.hasNext()) {
			JSONObject jo = new JSONObject();
			
			jo.put("lineStartPoint",it.next());
			j.add(jo);
		}

//		for (Object[] objects : line) {
//			JSONObject jo = new JSONObject();
//			jo.put("lineStartPoint", objects[0]);
//		}
		return j;
	}

	@Override
	public JSONArray findTranSta2(String lineStartPoint) {
		JSONArray j = new JSONArray();
		String hql  = " from Line as l where l.lineStartPoint=:lineStartPoint ";
		Query query = this.getCurrentSession().createQuery(hql);
		query.setString("lineStartPoint", lineStartPoint);
		
		List<Line> lines = query.list();
		for (Line line : lines) {
			JSONObject jo = new JSONObject();
			jo.put("lineEndPoint", line.getLineEndPoint());
			jo.put("lineTransportationType", line.getLineTransportationType());
			j.add(jo);
		}
		return j;
	}

	@Override
	public boolean insertOrderLine(String loStart, String loStop, String loTransferStation1, String loTransferStation2,
			int orId) {
		boolean flag = false;
		String sql = "insert into line_order(loStart,loStop,loTransferStation1,loTransferStation2,orId) "
				+ "values(:loStart,:loStop,:loTransferStation1,:loTransferStation2,:orId)";
		Query query = this.getCurrentSession().createSQLQuery(sql);
		query.setString("loStart",loStart );
		query.setString("loStop", loStop);
		query.setString("loTransferStation1",loTransferStation1 );
		query.setString("loTransferStation2",loTransferStation2 );
		query.setInteger("orId", orId);
		
		int i = query.executeUpdate();
		if (i>0) {
			flag = true;
		}
		return flag;
	}

}
